package com.zcxy.fast.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxy.fast.bean.PageQuery;
import com.zcxy.fast.entity.SysComponet;
import com.zcxy.fast.vo.SysComponetVo;
import com.zcxy.fast.service.ISysComponetService;
import com.zcxy.fast.mapper.SysComponetMapper;
import com.zcxy.fast.mybatisExtend.BaseServiceImpl;
import com.zcxy.fast.utils.BeanCopyUtil;
import com.zcxy.fast.utils.StringUtils;
import com.zcxy.fast.mybatisExtend.LambdaValiteCondition;
import com.zcxy.fast.mybatisExtend.RelationHelper;
import java.util.stream.Collectors;
import cn.hutool.core.collection.CollectionUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;



/**
 * vue组件数据配置业务层处理
 *
 * @author zcc
 * @email  860474958@qq.com
 * @date   2021-01-15 07:40:36
 */
@Service
@Slf4j
public class SysComponetServiceImpl extends BaseServiceImpl<SysComponetMapper, SysComponet> implements ISysComponetService {

    @Override
    public IPage<SysComponetVo> queryPageList(PageQuery<SysComponet> queryVo) {
        SysComponet sysComponet=queryVo.getT() == null ? new SysComponet() : queryVo.getT();
        QueryWrapper queryWrapper = new QueryWrapper(sysComponet);
        Page page = new Page(queryVo.getPageNo(), queryVo.getPageSize());
            if(StringUtils.isNotEmpty(sysComponet.getName())){
            queryWrapper.like("name",sysComponet.getName());
        }
            if(StringUtils.isNotEmpty(sysComponet.getCode())){
            queryWrapper.like("code",sysComponet.getCode());
        }
            if(StringUtils.isNotEmpty(sysComponet.getPostType())){
            queryWrapper.eq("post_type",sysComponet.getPostType());
        }
            queryWrapper.orderByDesc("create_time");


        IPage<SysComponet> iPage = baseMapper.selectPage(page, queryWrapper);
        return BeanCopyUtil.copyPage(iPage, SysComponetVo.class);
    }

    @Override
    public List<SysComponetVo> queryList(SysComponet sysComponet) {
        SysComponet sysComponetq=sysComponet == null ? new SysComponet() : sysComponet;
        QueryWrapper queryWrapper = new QueryWrapper(sysComponetq);
        List<SysComponetVo> SysComponetVos=BeanCopyUtil.copyListToList(baseMapper.selectList(queryWrapper),SysComponetVo.class);
        return SysComponetVos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSysComponet(SysComponet sysComponet) {
        LambdaValiteCondition<SysComponet> valiteCondition = LambdaValiteCondition.newInstance(sysComponet);
        valiteCondition.clear();
        valiteCondition.addField(SysComponet::getCode).addErrInfo("编码不能重复");
        validFiledExist(valiteCondition);
        baseMapper.insert(sysComponet);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSysComponet(SysComponet sysComponet) {
        LambdaValiteCondition<SysComponet> valiteCondition = LambdaValiteCondition.newInstance(sysComponet);
        valiteCondition.clear();
        valiteCondition.addField(SysComponet::getCode).addErrInfo("编码不能重复");
        valiteCondition.addExcludes(SysComponet::getId);
        validFiledExist(valiteCondition);


        baseMapper.updateById(sysComponet);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeAllSysComponet(List<String> ids) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids);
        baseMapper.delete(queryWrapper);
    }


    @Override
    public SysComponet viewSysComponet(String  code) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("code", code);
       return  baseMapper.selectOne(queryWrapper);
    }

}
